package org.paradise.dms.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.paradise.dms.pojo.CollegeInfo;
import org.paradise.dms.pojo.MajorInfo;
import org.paradise.dms.pojo.Student;
import org.paradise.dms.services.StudentService;
import org.paradise.dms.web.action.DMSBaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@SuppressWarnings("serial")
@Service
public class ValidateExcelFields extends DMSBaseAction {

	@Autowired
	private StudentService studentService;

	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public Student ExcelFieldsToValidate_to_Student(ExcelFieldsToValidate xcl) {
		Student s = new Student();
		s.setStudentno(xcl.getStudentno());
		s.setStudentname(xcl.getStudentname());
		s.setStudentenrollyear(xcl.getStudentenrollyear());
		s.setStudentidentitycardno(xcl.getStudentidentitycardno());
		if (!"".equals(xcl.getCollegeinfoid())) {
			s.setCollegeinfoid(Integer.parseInt(xcl.getCollegeinfoid())); // 学院code
																			// 可能要截字符串
		}
		if (!"".equals(xcl.getMajorinfoid())) {
			s.setMajorinfoid(Integer.parseInt(xcl.getMajorinfoid())); // 专业code
																		// 可能要截字符串
		}
		s.setStudentclass(xcl.getStudentclass());
		s.setStudentgender(xcl.getStudentgender());
		try {
			s.setStudentbirthday(new SimpleDateFormat("yyyy-MM-dd").parse(xcl.getStudentbirthday()));
		} catch (ParseException e1) {
			s.setStudentbirthday(null);
			e1.printStackTrace();
		}
		s.setStudentethnic(xcl.getStudentethnic());
		s.setStudentpolitics(xcl.getStudentpolitics());
		s.setStudentbirthplace(xcl.getStudentbirthplace());
		s.setStudentmiddleschool(xcl.getStudentmiddleschool());
		s.setStudentsecondlanguage(xcl.getStudentsecondlanguage());
		s.setStudenttype(xcl.getStudenttype());
		return s;
	}

	// ValidateExcelFields与string[]的互相转换
	public Object Object_VS_StringArr(ExcelFieldsToValidate xcl, String[] s, String flag) {
		if ("set".equals(flag)) {
			xcl.setErrorInfo(s[0]);
			xcl.setStudentno(s[1]);
			xcl.setStudentname(s[2]);
			xcl.setStudentenrollyear(s[3]);
			xcl.setStudenteducationyears(s[4]);
			xcl.setStudentidentitycardno(s[5]);
			xcl.setCollegeinfoid(s[6]);
			xcl.setMajorinfoid(s[7]);
			xcl.setStudentclass(s[8]);
			xcl.setStudentgender(s[9]);
			xcl.setStudentbirthday(s[10]);
			xcl.setStudentethnic(s[11]);
			xcl.setStudentpolitics(s[12]);
			xcl.setStudentbirthplace(s[13]);
			xcl.setStudentmiddleschool(s[14]);
			xcl.setStudentsecondlanguage(s[15]);
			xcl.setStudenttype(s[16]);
			return xcl;
		} else {
			s[0] = xcl.getErrorInfo();
			s[1] = xcl.getStudentno();
			s[2] = xcl.getStudentname();
			s[3] = xcl.getStudentenrollyear();
			s[4] = xcl.getStudenteducationyears();
			s[5] = xcl.getStudentidentitycardno();
			s[6] = xcl.getCollegeinfoid();
			s[7] = xcl.getMajorinfoid();
			s[8] = xcl.getStudentclass();
			s[9] = xcl.getStudentgender();
			s[10] = xcl.getStudentbirthday();
			s[11] = xcl.getStudentethnic();
			s[12] = xcl.getStudentpolitics();
			s[13] = xcl.getStudentbirthplace();
			s[14] = xcl.getStudentmiddleschool();
			s[15] = xcl.getStudentsecondlanguage();
			s[16] = xcl.getStudenttype();
			return s;
		}

	}

	@SuppressWarnings("unchecked")
	public ExcelFieldsToValidate validateExcelFields(ExcelFieldsToValidate s) {
		// 验证学号在数据库是否存在
		if ("" == (s.getStudentno())) {
			s.setError(true);
			s.setErrorInfo("学号不能为空");
			return s;

		} else {
			List<Student> sl = new ArrayList();
			sl = studentService.getStudentByStudentNo(s.getStudentno());
			if (sl != null && sl.size() > 0) {
				s.setError(true);
				s.setErrorInfo("学号在数据库中已存在");
				System.out.println(s.getStudentno() + "在数据库中");
				return s;
			}
		}
		// 验证姓名
		if ("" == (s.getStudentname())) {
			s.setError(true);
			s.setErrorInfo("姓名不能为空");
			return s;

		} else {
			// todo
		}
		// 验证入学年份
		if ("" == (s.getStudentenrollyear())) {
			s.setError(true);
			s.setErrorInfo("入学年份不能为空");
			return s;

		} else {
			// todo
		}
		// 验证学制
		if ("" == (s.getStudenteducationyears())) {
			s.setError(true);
			s.setErrorInfo("学制不能为空");
			return s;

		} else {
			// todo
		}
		// 验证身份证号码
		if ("" != (s.getStudenteducationyears())) {

		}
		// 验证学院是否存在
		if ("" == (s.getCollegeinfoid())) {
			// s.setError(true);
			// s.setErrorInfo("学院代码不能为空");
		} else {
			System.out.println(s.getCollegeinfoid() + "=学院id或code");
			boolean flag = false; // 学院存在标志
			List<CollegeInfo> colleges = new ArrayList();
			colleges = studentService.getAllcollegeInfo();
			for (int a = 0; a < colleges.size(); a++) {
				int collegeid = colleges.get(a).getCollegeinfoid();
				if (s.getCollegeinfoid().equals(collegeid + "")) {
					flag = true;
				}
			}
			if (flag == false) {
				s.setError(true);
				s.setErrorInfo("学院代码不存在");
				return s;
			}
		}
		// 验证专业是否存在
		if ("" == (s.getMajorinfoid())) {
			// s.setError(true);
			// s.setErrorInfo("专业代码不能为空");
		} else {
			System.out.print(s.getCollegeinfoid() + "=专业id或code");
			boolean flag = false; // 专业是否存在的标志
			List<MajorInfo> majors = new ArrayList();
			majors = studentService.getAllMajorInfo();
			for (int a = 0; a < majors.size(); a++) {
				if (s.getMajorinfoid().equals(majors.get(a).getMajorinfoid())) {
					flag = true;
				}
			}
			if (flag == false) {
				s.setError(true);
				s.setErrorInfo("专业代码不存在");
				return s;
			}
		}
		// 验证班级
		if ("" != (s.getStudentclass())) {

		}
		// 验证性别
		if ("" != (s.getStudentgender())) {
			if ((s.getStudentgender().indexOf("男") == -1) && (s.getStudentgender().indexOf("女") == -1)) {
				s.setError(true);
				s.setErrorInfo("性别输入有误");
				return s;
			}

		}
		// 生日验证
		if ("" != (s.getStudentbirthday())) {
			String str = s.getStudentbirthday();
			String regEx = "^(\\d{4})(0\\d{1}|1[0-2])(0\\d{1}|[12]\\d{1}|3[01])$";
			Pattern p = Pattern.compile(regEx);
			Matcher m = p.matcher(str);
			boolean b = m.matches();
			if (!b) {
				s.setError(true);
				s.setErrorInfo("生日格式输入有误");
				return s;
			}

		}
		// 民族验证
		if ("" != (s.getStudentethnic())) {

		}
		// 政治面貌验证
		if ("" != (s.getStudentpolitics())) {

		}
		// 生源地验证
		if ("" != (s.getStudentbirthplace())) {

		}
		// 毕业中学验证
		if ("" != (s.getStudentmiddleschool())) {

		}
		// 第二外语验证
		if ("" != (s.getStudentsecondlanguage())) {

		}
		// 层次验证
		if ("" != (s.getStudentethnic())) {

		}

		return s;
	}

}
